/* $RCSFile: IOHelper.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Helpers/IOHelper.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:28  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.IO;

namespace AddonNice.Helpers
{
	/// <summary>
	/// Summary description for IOHelper.
	/// </summary>
	public class IOHelper
	{
		/// <summary>
		/// Returns the names of files in the specified directory that match the specified searchpatterns.
		/// </summary>
		/// <param name="path">the directory to search.</param>
		/// <param name="searchPatterns">the search strings to match against the names of files in the path deliminated by a ';'. For example:"*.gif;*.xl?;my*.txt"</param>
		/// <returns></returns>
		public static string [] GetFiles(string path,string searchPatterns)
		{
			//declare the return array
			string[] returnArray = new string[0];
			
			if (System.IO.Directory.Exists(path))
			{
				//loop throuht the givven searchpatterns
				foreach (string ext in searchPatterns.Split(';'))
				{

					string [] tmpArray ;
					tmpArray = System.IO.Directory.GetFiles(path,ext);
					if (tmpArray.Length >0 )
					{
						string [] newArray = new string[returnArray.Length + tmpArray.Length];
						returnArray.CopyTo(newArray,0);
						tmpArray.CopyTo(newArray,returnArray.Length);
						returnArray = newArray;
					}
				}
			}
			return returnArray;						
		}
		/// <summary>
		/// Creates all directories and subdirectories as specified and return false in case of an error.
		/// </summary>
		/// <param name="physicalPath"></param>
		/// <returns></returns>
		public static bool CreateDirectory(string physicalPath)
		{
			bool returnValue = true;

			if (!System.IO.Directory.Exists(physicalPath)) 
			{
				try { System.IO.Directory.CreateDirectory(physicalPath);}
				catch { returnValue = false; }
			}
			return returnValue;
		}
	}
}
